﻿var page = {
    monthList: [
        { xStart: 3, count: 31, name: "Jan." }, //1
        { xStart: 6, count: 28, name: "Feb." }, //2
        { xStart: 6, count: 31, name: "Mar." }, //3
        { xStart: 2, count: 30, name: "Apr." }, //4
        { xStart: 4, count: 31, name: "May." }, //5
        { xStart: 7, count: 30, name: "Jun." }, //6
        { xStart: 2, count: 31, name: "Jul." }, //7
        { xStart: 5, count: 31, name: "Aug." }, //8
        { xStart: 1, count: 30, name: "Sept." }, //9
        { xStart: 3, count: 31, name: "Oct." }, //10
        { xStart: 6, count: 30, name: "Nov." }, //11
        { xStart: 1, count: 31, name: "Dec." }, //12
    ],

    addDays: function (xStart, count) {
        this.dayList = new Array();
        for (var i = 1; i <= count; i++ , xStart++) {
            this.setData({ calendar: { page: this, item: "Panels/dayItem" }, });
            var dayItem = {
                dayItem: {
                    id: "day" + i,
                    position: { x: 21 + (xStart - 1) % 7 * 67, y: 83 + parseInt((xStart - 1) / 7) * 38 },
                    hide: true
                },
            };
            this.dayList.push(dayItem);
            var buttomItem = { button1: { value: i.toString(), id: "dayBtn" + i } }
            this.setData(dayItem);
            this.setData(buttomItem);
        };

    },

    onAnimate1: function () { //逐个进入

        perStart = 1;
        perFrame = 4;

        total = this.dayList.length * perStart + perFrame;
        console.log("total  spend : " + total * 30);
        anim = pm.createAnimation(total, 30);

        for (var i = 0; i < this.dayList.length; i++) {
            var item = this.dayList[i].dayItem;
            frameStart = i * perStart;
            frameEnd = i * perStart + perFrame;

            move = anim.move(item.id, item.position.x + 40, item.position.y, item.position.x, item.position.y).fade();
            move.fade();
            move.progress(frameStart, frameEnd);
            var data = new Object();
            data[item.id] = { hide: false };
            this.setData(data);
        }
        anim.start();
    },

    onAnimate2: function (xStart, count) { //按列进入

        var dayList = new Array();
        dayList.list0 = new Array();
        dayList.list1 = new Array();
        dayList.list2 = new Array();
        dayList.list3 = new Array();
        dayList.list4 = new Array();
        dayList.list5 = new Array();
        dayList.list6 = new Array();

        for (var i = 0; i < count; i++ , xStart++) {
            var item = this.dayList[i].dayItem;
            dayList["list" + (xStart - 1) % 7].push(item);
        }

        perStart = 3;
        perFrame = 8;
        total = 7 * perStart + perFrame;
        //console.log("total  spend : " + total*30);
        anim = pm.createAnimation(total, 30);
        var moveList = new Object();
        var data = new Object();
        for (i = 0; i < 7; i++) {
            frameStart = i * perStart;
            frameEnd = i * perStart + perFrame;

            var tempList = dayList["list" + i];

            for (j = 0; j < tempList.length; j++) {
                console.dir(tempList[j]);
                var move = anim.move(tempList[j].id,
                    tempList[j].position.x,
                    tempList[j].position.y - 40,
                    tempList[j].position.x,
                    tempList[j].position.y);
                move.progress(frameStart, frameEnd);
                move.fade();
                var data = new Object();
                data[tempList[j].id] = { hide: false };
                this.setData(data);
            }
        }

        anim.start();
    },

    onAnimate3: function (xStart, count) { //按列进入

        var dayList = new Array();
        dayList.list0 = new Array();
        dayList.list1 = new Array();
        dayList.list2 = new Array();
        dayList.list3 = new Array();
        dayList.list4 = new Array();

        for (var i = 0; i < count; i++ , xStart++) {
            var item = this.dayList[i].dayItem;
            if (parseInt((xStart - 1) / 7) > 4)
                dayList["list" + 0].push(item);
            else
                dayList["list" + parseInt((xStart - 1) / 7)].push(item);
        }

        perStart = 3;
        perFrame = 8;
        total = 5 * perStart + perFrame;
        console.log("total  spend : " + total * 30);
        anim = pm.createAnimation(total, 30);
        var moveList = new Object();
        var data = new Object();
        for (i = 0; i < 5; i++) {
            frameStart = i * perStart;
            frameEnd = i * perStart + perFrame;

            var tempList = dayList["list" + i];

            for (j = 0; j < tempList.length; j++) {
                console.dir(tempList[j]);
                var move = anim.move(tempList[j].id,
                    tempList[j].position.x + 40,
                    tempList[j].position.y,
                    tempList[j].position.x,
                    tempList[j].position.y);
                move.progress(frameStart, frameEnd);
                move.fade();
                var data = new Object();
                data[tempList[j].id] = { hide: false };
                this.setData(data);
            }
        }
        anim.start();
    },

    changeDayPosition: function (xStart, count) {
        for (var i = 0; i < this.dayList.length; i++) {
            var data = new Object();
            data[this.dayList[i].dayItem.id] = { hide: true };
            this.setData(data);
        }
        for (var i = 0; i < count; i++ , xStart++) {
            var item = this.dayList[i].dayItem;

            item.position.x = 21 + (xStart - 1) % 7 * 67;
            item.position.y = 83 + parseInt((xStart - 1) / 7) * 38;

            if (parseInt((xStart - 1) / 7) > 4)
                item.position.y = 83;
        };
    },

    month: 1,
    chooseWay: false,
    /* 此方法在第一次显示窗体前发生 */
    onLoad: function (event) {
        var thiz = this;
        this.timer = setTimeout(function () {
            var PositionInfo = thiz.monthList[event.detail.value]
            thiz.changeDayPosition(PositionInfo.xStart, PositionInfo.count);
            switch (event.detail.value % 2) {
                case 1:
                    thiz.onAnimate3(PositionInfo.xStart, PositionInfo.count);
                    break;
                case 0:
                    thiz.onAnimate2(PositionInfo.xStart, PositionInfo.count);
                    break;
                default:
                    break;
            }

        }, 100)
    },

    /* 此方法展示窗体后发生 */
    onResume: function (event) {

    },

    /* 当前页状态变化为显示时触发 */
    onShow: function (event) {

    },

    /* 当前页状态变化为隐藏时触发 */
    onHide: function (event) {

    },

    /* 此方法关闭窗体前发生 */
    onExit: function (event) {
        this.addDays(3, 31);
        this.onAnimate3(3, 31);
    },

    onBtn: function (event) {
        switch (event.target.id) {
            case "back":
                if (typeof (this.CircleTimer) != "undefined")
                    clearInterval(this.CircleTimer);
                pm.navigateBack();
                return;
            case "before":
                this.month -= 1;
                if (this.month < 1)
                    this.month = 12;
                break;
            case "after":
                this.month += 1;
                if (this.month > 12)
                    this.month = 1;
                break;
            default:
                return;
        }
        var PositionInfo = this.monthList[this.month - 1];
        this.setData({ month: { value: PositionInfo.name, refresh: true } });
        this.changeDayPosition(PositionInfo.xStart, PositionInfo.count);
        this.onAnimate3(PositionInfo.xStart, PositionInfo.count);
    },
};

Page(page);

page = 0;
